/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace MyPhotoIndex.Controls
{
    public partial class MyPhotoIndexSplitContainer : SplitContainer
    {
        private int m_topMargin;
        private int m_bottomMargin;

        private static Image ms_gripImage = Resources.Resources.horisplitGrip;

        [Description("Top margin")]
        [Category("SplitPanel")]
        public int TopMargin
        {
            get { return m_topMargin; }
            set { m_topMargin = value; }
        }

        [Description("Bottom margin")]
        [Category("SplitPanel")]
        public int BottomMargin
        {
            get { return m_bottomMargin; }
            set { m_bottomMargin = value; }
        }

        public MyPhotoIndexSplitContainer()
        {
            InitializeComponent();
            this.Panel1.Paint += new PaintEventHandler(Panel1_Paint);
            this.Panel2.Paint += new PaintEventHandler(Panel2_Paint);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Rectangle splitterRect = this.SplitterRectangle;
            if (splitterRect.Height <= 1 || 
                splitterRect.Width <= 1)
            {
                return;
            }

            Brush brush = null;
            if (this.Orientation == System.Windows.Forms.Orientation.Vertical)
            {
                Color firstColor = Color.FromArgb(225, 238, 255);
                Color secondColor = Color.FromArgb(155, 177, 208);

                brush = new LinearGradientBrush(splitterRect,
                    firstColor, secondColor, LinearGradientMode.Vertical);
            }
            else
            {
                Color firstColor = Color.FromArgb(225, 238, 255);
                Color secondColor = Color.FromArgb(145, 169, 197);

                Rectangle rect = splitterRect;
                rect.Y++;
                rect.Height--;
                brush = new LinearGradientBrush(rect,
                    firstColor, secondColor, LinearGradientMode.Vertical);
            }

            using (brush)
            {
                e.Graphics.FillRectangle(brush, splitterRect);
            }

            Color borderColor = Color.FromArgb(79, 94, 113);

            if (this.Orientation == System.Windows.Forms.Orientation.Vertical)
            {
                using (Pen pen = new Pen(borderColor))
                {
                    int y = splitterRect.Y + Panel1.Padding.Top - 1;
                    int x = splitterRect.X;
                    e.Graphics.DrawLine(pen, x, y, x, splitterRect.Height - Panel1.Padding.Top);
                    x += (splitterRect.Width - 1);
                    e.Graphics.DrawLine(pen, x, y, x, splitterRect.Height - Panel1.Padding.Top);
                }
            }
            else
            {
                using (Pen pen = new Pen(borderColor))
                {
                    int y = splitterRect.Y;
                    int x = splitterRect.X;
                    e.Graphics.DrawLine(pen, x, y, x + splitterRect.Width, y);
                    y += (splitterRect.Height - 1);
                    e.Graphics.DrawLine(pen, x, y, +splitterRect.Width, y);
                }

                Rectangle gripRect = new Rectangle((splitterRect.Width - ms_gripImage.Width) / 2, splitterRect.Y+1, ms_gripImage.Width, ms_gripImage.Height);
                e.Graphics.DrawImageUnscaled(ms_gripImage, gripRect);
            }
        }

        void Panel2_Paint(object sender, PaintEventArgs e)
        {
            Rectangle drawRect = this.Panel2.ClientRectangle;
            if (drawRect == Rectangle.Empty)
            {
                return;
            }

            Color firstColor = Color.FromArgb(225, 238, 255);
            Color secondColor = Color.FromArgb(155, 177, 208);

            Brush brush = new LinearGradientBrush(drawRect, firstColor, secondColor, LinearGradientMode.Vertical);

            using (brush)
            {
                e.Graphics.FillRectangle(brush, drawRect);
            }

            Color borderColor = Color.FromArgb(79, 94, 113);

            using (Pen pen = new Pen(borderColor))
            {
                if (this.RightToLeft == RightToLeft.No)
                {
                    int y = drawRect.Y + this.Panel2.Padding.Top - 1;
                    e.Graphics.DrawLine(pen, drawRect.X, y, drawRect.Width - this.Panel2.Padding.Right, y);

                    int x = drawRect.X + drawRect.Width - this.Panel2.Padding.Right;
                    e.Graphics.DrawLine(pen, x, this.Panel2.Padding.Top - 1, x, this.Panel2.Height - this.Panel2.Padding.Bottom);

                    y = drawRect.Height - this.Panel1.Padding.Top;
                    e.Graphics.DrawLine(pen, drawRect.X, y, drawRect.Width - this.Panel2.Padding.Right, y);
                }
                else
                {
                    int y = drawRect.Y + this.Panel2.Padding.Top - 1;
                    e.Graphics.DrawLine(pen, drawRect.X + this.Panel2.Padding.Left, y, drawRect.Width, y);

                    int x = drawRect.X + this.Panel2.Padding.Left - 1;
                    e.Graphics.DrawLine(pen, x, this.Panel2.Padding.Top - 1, x, this.Panel2.Height - this.Panel2.Padding.Bottom);

                    y = drawRect.Height - this.Panel1.Padding.Top;
                    e.Graphics.DrawLine(pen, x, y, drawRect.Width, y);
                }
            }
        }

        void Panel1_Paint(object sender, PaintEventArgs e)
        {
            Rectangle panelDrawRect = this.Panel1.ClientRectangle;
            if (panelDrawRect.Width == 0 ||
                panelDrawRect.Height == 0)
            {
                return;
            }

            Color borderColor = Color.FromArgb(79, 94, 113);

            Color firstColor = Color.FromArgb(225, 238, 255);
            Color secondColor = Color.FromArgb(155, 177, 208);

            Brush brush = new LinearGradientBrush(panelDrawRect, firstColor, secondColor, LinearGradientMode.Vertical);

            using (brush)
            {
                e.Graphics.FillRectangle(brush, panelDrawRect);
            }

            using (Pen pen = new Pen(borderColor))
            {
                if (this.RightToLeft == RightToLeft.No)
                {
                    int y = panelDrawRect.Y + this.Panel1.Padding.Top - 1;
                    e.Graphics.DrawLine(pen, panelDrawRect.X + this.Panel1.Padding.Left, y, panelDrawRect.Width, y);

                    int x = panelDrawRect.X + this.Panel1.Padding.Left - 1;
                    e.Graphics.DrawLine(pen, x, this.Panel1.Padding.Top - 1, x, this.Panel1.Height - this.Panel1.Padding.Bottom);

                    y = panelDrawRect.Height - this.Panel1.Padding.Top;
                    e.Graphics.DrawLine(pen, x, y, panelDrawRect.Width, y);
                }
                else
                {
                    int y = panelDrawRect.Y + this.Panel1.Padding.Top - 1;
                    e.Graphics.DrawLine(pen, panelDrawRect.X, y, panelDrawRect.Width - this.Panel1.Padding.Right, y);

                    int x = panelDrawRect.X + panelDrawRect.Width - this.Panel1.Padding.Right;
                    e.Graphics.DrawLine(pen, x, this.Panel1.Padding.Top - 1, x, this.Panel1.Height - this.Panel1.Padding.Bottom);

                    y = panelDrawRect.Height - this.Panel1.Padding.Top;
                    e.Graphics.DrawLine(pen, x, y, panelDrawRect.X, y);
                }
            }
        }
    }
}
